package com.juyu.miliao.api.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.vo.GiftSpendVo;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.util.Md5Encryption;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.domain.GiftSpend;
import com.juyu.miliao.api.service.GiftSpendService;
import com.juyu.miliao.api.validator.GiftSpendCheck;
import com.juyu.miliao.api.validator.GiveSpendCheck;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 礼物来往
 * 
 * @author wbf
 */
@RestController
@RequestMapping("giftSpend")
public class GiftSpendController {
	@Resource
	GiftSpendService giftSpendService;


	/**
	 * 查看用户接收的礼物
	 */
	@GetMapping("/{inUserId:^[1-9][0-9]*$}/{page}-{pageSize}")
	@JsonView(JacksonView.GiftSpend.class)
	public List<GiftSpend> findGiftSpendByInUserId(@PathVariable("inUserId") int inUserId,
			@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return giftSpendService.findGiftSpendByInUserId(inUserId, page, pageSize);
	}

	/**
	 * 赠送礼物
	 */
	@PostMapping
	public void saveGiftSpend(Authentication token, HttpServletRequest request,
									HttpServletResponse response,
									@Validated(GiftSpendCheck.class) GiftSpend entity) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != entity.getOutUserId())
			throw new IllegalParamException("用户id错误");
		giftSpendService.saveGiftSpend(entity);
		//2019-10-11 增加混淆
		CommonHead com = VerifyUtil.verifyCommonHead(request);
		if(!StringUtils.isEmpty(com.getRanReqPk())){
			response.setHeader("ranReqPk",com.getRanReqPk());
			response.setHeader("ranResPk",Md5Encryption.getDigestStr(com.getRanReqPk()));
		}
	}

	/**
	 * 赠送金币
	 */
	@PostMapping("/spend")
	public void GiveSpend(Authentication token, HttpServletRequest request,
						  HttpServletResponse response,
						  @Validated(GiveSpendCheck.class) BillDetail entity) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != entity.getOutUserId())
			throw new IllegalParamException("用户id错误");
		giftSpendService.saveGiveSpend(entity);
		//2019-10-11 增加混淆
		CommonHead com = VerifyUtil.verifyCommonHead(request);
		if(!StringUtils.isEmpty(com.getRanReqPk())){
			response.setHeader("ranReqPk",com.getRanReqPk());
			response.setHeader("ranResPk",Md5Encryption.getDigestStr(com.getRanReqPk()));
		}
	}

	/**
	 * 获取主播单个礼物详情
	 * @param token
	 * @param giftId
	 * @return
	 */
	@GetMapping("/gift/{giftId}/{page}-{pageSize}")
	public List<GiftSpendVo> getUserGiftList(Authentication token,
											 @PathVariable("giftId")int giftId,
											 @PathVariable("page") Integer page,
											 @PathVariable("pageSize") Integer pageSize){
		if (page <= 0) {
			page = 1;
		}
		pageSize = 20;
		Subject subject = (Subject) token.getPrincipal();
		return giftSpendService.getUserGiftList(subject.getId(),giftId,page,pageSize);
	}

}
